﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 蓝牙串口
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 将所有的端口加载到下拉选项中
            foreach (string portname in SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(portname);
            }
            // 默认选中第一个端口
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedItem = comboBox1.Items[0];
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem == null)
            {
                return;
            }
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            serialPort.PortName = (string)comboBox1.SelectedItem;
            serialPort.Encoding = Encoding.UTF8;
            serialPort.BaudRate = 115200;
            serialPort.StopBits = StopBits.One;
            serialPort.Parity = Parity.None;
            try
            {
                serialPort.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void SerialWrite(string str)
        {
            if (this.serialPort.IsOpen)
            {
                serialPort.Write(str);
            }
            else
            {
                MessageBox.Show("发送失败，串口无法连接！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SerialWrite("1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SerialWrite("2");
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SerialWrite("3");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SerialWrite("4");
        }
        
        private void ButtonRefresh_Click(object sender, EventArgs e)
        {
            // 保存上次修改的结果
            string lastselection = comboBox1.SelectedText;
            comboBox1.Items.Clear();
            // 将所有的端口加载到下拉选项中
            foreach (string portname in SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(portname);
            }
            // 默认选中第一个端口
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedItem = comboBox1.Items[0];
            }
            // 恢复之前选择的端口
            if (lastselection != null)
            {
                comboBox1.SelectedText = lastselection;
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.D1:
                    this.button1_Click(null, null);
                    break;
                case Keys.D2:
                    this.button2_Click(null, null);
                    break;
                case Keys.D3:
                    this.button3_Click(null, null);
                    break;
                case Keys.D4:
                    this.button4_Click(null, null);
                    break;
                default:
                    break;
            }
        }
    }
}